package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <h3>health94_pareant</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-03-17 20:56
 **/

@RestController
@RequestMapping("checkitem")
@PreAuthorize("hasAnyRole('ROLE_ADMIN')")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("add")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        try {
            checkItemService.addCheckItem(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = checkItemService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, pageResult);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL, null);
        }
    }

    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id")Integer id){
                try {
                    checkItemService.deleteByCode(id);
                    return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
                } catch (RuntimeException e){
                    return new Result(false, e.getMessage());
                } catch (Exception e) {
                    return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
                }


    }
    @RequestMapping("update")
    public Result update(@RequestBody CheckItem checkItem){
        try {
            checkItemService.update(checkItem);
            return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("findAllCheckItem")
    public Result findAllCheckItem(){
        try {
            List<CheckItem> list = checkItemService.findAllCheckItem();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

}
